//
//  SetupView.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/27.
//

import UIKit

class SetupView: UIView {
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubviews([
            emailView,
            pwdView,
            rePwdView
        ])
        emailView.addSubviews([
            emailImageV,
            emailTextF
        ])
        pwdView.addSubviews([
            pwdImageV,
            pwdTextF,
            showBtn
        ])
        rePwdView.addSubviews([
            rePwdImageV,
            rePwdTextF,
            reShowBtn
        ])
        
        emailView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(42)
            make.top.equalToSuperview()
        }
        emailImageV.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.leading.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        emailTextF.snp.makeConstraints { make in
            make.height.equalTo(30)
            make.leading.equalToSuperview().offset(55)
            make.trailing.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
        }
        pwdView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(42)
            make.top.equalTo(emailView.snp.bottom).offset(30)
        }
        pwdImageV.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.leading.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        pwdTextF.snp.makeConstraints { make in
            make.height.equalTo(30)
            make.leading.equalToSuperview().offset(55)
            make.trailing.equalToSuperview().offset(-55)
            make.centerY.equalToSuperview()
        }
        showBtn.snp.makeConstraints { make in
            make.height.width.equalTo(24)
            make.trailing.equalToSuperview().offset(-13)
            make.centerY.equalToSuperview()
        }
        rePwdView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(42)
            make.top.equalTo(pwdView.snp.bottom).offset(30)
        }
        rePwdImageV.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.leading.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        rePwdTextF.snp.makeConstraints { make in
            make.height.equalTo(30)
            make.leading.equalToSuperview().offset(55)
            make.trailing.equalToSuperview().offset(-55)
            make.centerY.equalToSuperview()
        }
        reShowBtn.snp.makeConstraints { make in
            make.height.width.equalTo(24)
            make.trailing.equalToSuperview().offset(-13)
            make.centerY.equalToSuperview()
        }
    }
    
    @objc private func clickShowPwd(){
        
        showBtn.isSelected = !showBtn.isSelected
        pwdTextF.isSecureTextEntry = !showBtn.isSelected
    }
    @objc private func clickShowRePwd(){
        
        reShowBtn.isSelected = !reShowBtn.isSelected
        rePwdTextF.isSecureTextEntry = !reShowBtn.isSelected
    }
    
    @objc func rePwdFieldDidChange(_ textField: UITextField) {
        
        if let text = rePwdTextF.text, text.count > 16 {
            rePwdTextF.text = String(text.prefix(16))
        }
    }
    
    @objc func textFieldDidChange(_ textField: UITextField) {
        
        if let text = pwdTextF.text, text.count > 16 {
            pwdTextF.text = String(text.prefix(16))
        }
    }
    
    private lazy var emailView: ShadowView = {
        let v = ShadowView()
        v.cornerRadius = 8
        v.shadowColor = UIColor(hexString: "#333333", alpha: 0.5)
        v.shadowOpacity = 0.5
        v.shadowOffset = CGSize(width: 0, height: 2)
        v.shadowRadius = 5
        v.backgroundColor = .white
        return v
    }()
    private lazy var pwdView: ShadowView = {
        let v = ShadowView()
        v.cornerRadius = 8
        v.shadowColor = UIColor(hexString: "#333333", alpha: 0.5)
        v.shadowOpacity = 0.5
        v.shadowOffset = CGSize(width: 0, height: 2)
        v.shadowRadius = 5
        v.backgroundColor = .white
        return v
    }()
    private lazy var rePwdView: ShadowView = {
        let v = ShadowView()
        v.cornerRadius = 8
        v.shadowColor = UIColor(hexString: "#333333", alpha: 0.5)
        v.shadowOpacity = 0.5
        v.shadowOffset = CGSize(width: 0, height: 2)
        v.shadowRadius = 5
        v.backgroundColor = .white
        return v
    }()
    private lazy var emailImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "login_email")
        return v
    }()
    lazy var emailTextF: UITextField = {
        let v = UITextField()
        v.placeholder = "Kontonummer"
        v.textColor = .black
        v.font = UIFont.systemFont(ofSize: 16)
        v.backgroundColor = .clear
        return v
    }()
    private lazy var pwdImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "login_pwd")
        return v
    }()
    lazy var pwdTextF: UITextField = {
        let v = UITextField()
        v.placeholder = "Passwort: 6-16 Zeichen"
        v.textColor = .black
        v.font = UIFont.systemFont(ofSize: 16)
        v.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        v.backgroundColor = .clear
        v.isSecureTextEntry = true  // 密码模式
        v.textContentType = .oneTimeCode

        return v
    }()
    private lazy var showBtn: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage(UIImage(named: "login_hidden"), for: .normal)
        v.setImage(UIImage(named: "login_show"), for: .selected)
        v.addTarget(self, action: #selector(clickShowPwd), for: .touchUpInside)
        return v
    }()
    private lazy var rePwdImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "login_pwd")
        return v
    }()
    lazy var rePwdTextF: UITextField = {
        let v = UITextField()
        v.placeholder = "Passwort bestätigen"
        v.textColor = .black
        v.font = UIFont.systemFont(ofSize: 16)
        v.addTarget(self, action: #selector(rePwdFieldDidChange(_:)), for: .editingChanged)
        v.backgroundColor = .clear
        v.isSecureTextEntry = true  // 密码模式

        return v
    }()
    private lazy var reShowBtn: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage(UIImage(named: "login_hidden"), for: .normal)
        v.setImage(UIImage(named: "login_show"), for: .selected)
        v.addTarget(self, action: #selector(clickShowRePwd), for: .touchUpInside)
        return v
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
